Command Line


Posted by yunanpan on 2020-06-12

什麼是 Command Line Interface(CLI)?

Command line 是一種以文字指令操控電腦的方法。因為不是所有的程式都需要視窗介面來操作。像是連接雲端主機時並沒有視窗可用,所以便需要以 command line 來執行。
而不同於 Command line,Graphical User Interface(GUI) 則是利用視窗介面來操作電腦(一般習慣使用的介面)。


以下單純記錄一下 Command Line 常用到的指令。

pwd 印出所在位置
ls 印出現在所在資料夾底下的檔案
cd 切換資料夾

cd <folder> // 切換到 folder
cd .. // 回上一層

man 使用說明書
(MAC 限定,WINDOWS 作業系統需另外安裝。我自己太懶都用 --help 來查詢指令功能。)
touch 建立檔案或更改時間

// 如果原來就有 test.txt 的檔案
 touch test.txt // 更改檔案儲存的時間
 // 如果 touch 一個不存在的檔案
 touch newtest.txt // 新增檔案

rm 刪除檔案
如果要刪除資料夾,則需要使用 rmdir
如果沒有 rmdir,可用 rm -r 來刪除資料夾。惟需要注意使用 rm -r 指令時,會連資料夾裡的檔案都一併刪除。而使用 rmdir 刪除資料夾時,如果欲刪除的資料夾裡面有東西時,則會跳出 Directory not empty 的警告。需先清空資料夾,才得以成功刪除。
mkdir 建立資料夾
mv 移動檔案或是改名

mv <要移動的檔案> <目標位置>
mv <要改名的檔案> <新名稱>

cp 複製檔案

cp <原檔> <複製檔>
// 複製資料夾
cp -r <原資料夾> <複製資料夾>

grep 抓取關鍵字

grep <keyword> <檔案>

git bash 的關鍵字沒有 highlight 起來,可以使用 grep --color=always

wget 下載檔案(也可下載網頁原始碼)
(需另外下載)
curl 送出 request,可以拿來測試 API
cat 連接檔案,也可以用來顯示檔案內容
less 分頁式印出檔案(可用上下鍵查看),適用於檔案內容很長時使用,按 Q 離開
echo 印出字串
date 印出現在的時間

/ 根目錄
~ Home 目錄


組合指令

redirection > >> 重新導向 input output

ls -al > list_result
// 把 ls -al 的 output 導到 list_result 的檔案裡
// 原本 output 會直接呈現在 command line 上
echo "123" > 123.txt // 123
echo "456" > 123.txt // 456 (使用 > 會直接覆蓋)
echo "789" >> 123.txt // 456 789 (使用 >> 會以 append 的方式新增內容到檔案中)

pipe | 左邊指令的 output 會變成右邊指令的 input

// hello 的內容
hello
hihihihi
cat hello // 呈現上述 hello 的內容
cat hello | grep he
// 把 hello 的內容丟到 pipe 的右邊執行 grep he,最後會呈現抓取關鍵字後的成果
// hello

Vim

vim:文字編輯器
vim <檔案>
i insert
esc 離開 insert 的狀態
:wq 離開(windows用 :q 離不開)(需要在非 insert 的狀況下才能執行)










Related Posts

Command Line 筆記

Command Line 筆記

redis 套件的 Property 'on' does not exist on type 'RedisClientType'

redis 套件的 Property 'on' does not exist on type 'RedisClientType'

JavaScirpt - async/await

JavaScirpt - async/await


Comments